#!/bin/sh
### BEGIN INIT INFO
# Provides:          softcam
# Required-Start:    $local_fs
# Required-Stop:     
# Should-Start:      $time $remote_fs cardserver
# Should-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: oscam
# Description:       oscam SoftCAM, card reader and server.
### END INIT INFO

readlink $0 > /dev/null 2>&1 && CAM=$(basename $(readlink $0) | sed s#^softcam\.##)

[ -z "$CAM" ] && CAM=$(basename $0 | sed s#^softcam\.##)

if [ -z "$CAM" -o "$CAM" == "softcam" ]; then
	unset CAM
	for TESTCAM in oscam oscam-schimmelreiter oscam-schimmelreiter-ipv4 oscam-atv
	do
		[ -x /usr/bin/$TESTCAM ] && CAM=$TESTCAM && break
	done
fi

[ -n "$CAM" ] || exit 1
[ -x /usr/bin/$CAM ] || exit 1

PIDFILE=/var/tmp/${CAM}.pid
DESC="Softcam service $CAM"
DAEMON=/usr/bin/$CAM

case "$1" in
start)
	if [ -e $PIDFILE ]; then
		PIDDIR=/proc/$(cat $PIDFILE)
		if [ -d ${PIDDIR} ] && [[ $(readlink -f ${PIDDIR}/exe) == $DAEMON ]]; then
			echo "$DESC already started; not starting."
			exit 1
		else
			rm -f $PIDFILE
		fi
	fi
	echo -n "Starting $DESC: "
	ulimit -s 1024
	$DAEMON --config-dir /etc/tuxbox/config/$CAM --daemon --pidfile $PIDFILE --restart 2 --utf8 2| grep -v "UTF-8 mode"
	sleep 0.5
	RETVAL=1
	[ -e $PIDFILE ] && RETVAL=0
	if [ "$RETVAL" -eq "0" ]; then
		echo "OK"
	else
		echo "FAILED"
	fi
	exit $RETVAL
	;;
stop)
	echo -n "Stopping $DESC: "
	kill `cat $PIDFILE 2> /dev/null` 2> /dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq "0" ]; then
		echo "OK"
	else
		echo "FAILED"
	fi
	sleep 1
	killall $CAM 2> /dev/null
	exit $RETVAL
	;;
restart|reload)
	$0 stop
	sleep 1
	$0 start
	exit $?
	;;
version)
	$CAM -V | grep 'Version\|IPv6' | sed 's/Version:[ ]*//' | sed 's/IPv6.*yes/with IPv6/g' | sed 's/IPv6.*no/IPv4-only/g' | sed ':a;N;$!ba;s/\n/ /g'
	;;
info)
	$CAM -V | grep 'Version\|IPv6' | sed 's/Version:[ ]*//' | sed 's/IPv6.*yes/with IPv6/g' | sed 's/IPv6.*no/IPv4-only/g' | sed ':a;N;$!ba;s/\n/ /g'
	;;
*)
	echo "Usage: $0 start|stop|restart"
	exit 1
	;;
esac
exit 0
